package action;

import Impl.ReplyDaoImpl;
import Impl.TopicDaoImpl;
import Impl.plateDaoImpl;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "AddReply",urlPatterns = "/addreply.jhtml")
public class AddReply extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date currentTime = new Date();
        request.setCharacterEncoding("utf-8");
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        int tid=Integer.parseInt(request.getParameter("rtid"));
        int sid=Integer.parseInt(request.getParameter("sid"));
        String sname=request.getParameter("sname");
        request.setAttribute("sname",sname);
        String topic=request.getParameter("tname");
        String content=request.getParameter("content");
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("user");
        String a=  request.getParameter("author");
        System.out.println(a);
        int uid=user.getUid();
        ReplyDaoImpl replyDao=new ReplyDaoImpl();
        replyDao.insertReplyInfo(topic,content,tid,sid,uid);
        String url="/showarticle.jhtml?tid="+tid+"&author="+a+"&&sname="+sname;
        System.out.println(url);
        new TopicDaoImpl().getRepltCountByid(tid);
        request.getRequestDispatcher(url).forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
